import Notify from '@vant/weapp/notify/notify';
import Dialog from '@vant/weapp/dialog/dialog';
import {request} from '../../assets/js/request.js';
Page({
  _getClassInfo: function() {
    wx.showLoading({
      title: '正在加载',
      mask: true,
    })
    request('/getClassInfo.php', { uid: this.data.uid, status: this.data.status}
    ).then((result) => {
      this.setData({
        classItems: result.data.list
      })
    }).catch((error) => {
      console.log(error)
      Notify({
        type: 'danger',
        message: '服务器繁忙，请稍后再试'
      });
    })
  },
  _getApplyList: function() {
    request('/getApplyList.php', {status: this.data.status, uid: this.data.uid})
    .then((res)=>{
      this.setData({
        applyList: res.data.list
      })
    })
  },
  applyPass: function(e) {
    wx.showLoading({
      title: '请稍后',
      mask: true,
    })
    request('/handleApply.php', { id: e.detail.id, state: 'pass'})
    .then((res)=>{
      Notify({
        type: 'success',
        message: '操作成功'
      });
      this._getApplyList();
    })
    .catch((err)=>{
      Notify({
        type: 'danger',
        message: '服务器繁忙，请稍后再试'
      });
    })
  },
  applyRefuse: function(e) {
    wx.showLoading({
      title: '请稍后',
      mask: true,
    })
    request('/handleApply.php', { id: e.detail.id, state: 'refuse' })
      .then((res) => {
        Notify({
          type: 'success',
          message: '操作成功'
        });
        this._getApplyList();
      })
      .catch((err) => {
        Notify({
          type: 'danger',
          message: '服务器繁忙，请稍后再试'
        });
      })
  },
  cancelApply: function(e) {
    wx.showLoading({
      title: '请稍后',
      mask: true,
    })
    let cancelData = {
      classId: e.detail.classId,
      uId: this.data.uid
    }
    request('/cancelApply.php', cancelData)
      .then((res) => {
        if (res.data == 'ok') {
          Notify({
            type: 'success',
            message: '取消成功'
          });
          this._getApplyList();
        } else if (res.data == 'fail') {
          Notify({
            type: 'danger',
            message: '操作失败'
          });
        }
      })
      .catch((err) => {
        Notify({
          type: 'danger',
          message: '服务器繁忙，请稍后再试'
        });
      })
  },

  delClass:function(e){
    Dialog.confirm({
      title: '注意',
      message: '课程删除后数据无法恢复，您确定要删除该课程吗？'
    }).then(() => {
      wx.showLoading({
        title: '正在删除',
        mask: true,
      })
      let delData = {
        uid: this.data.uid,
        classId: e.detail.classId,
        status: this.data.status
      }
      request('/delClass.php', delData)
        .then((res) => {
          if (res.data == 'ok') {
            Notify({ type: 'success', message: '删除成功' });
            this._getClassInfo();
          } else if (res.data == 'fail') {
            Notify({ type: 'warning', message: '删除失败，请稍后再试' });
          }
        })
        .catch((err) => {
          Notify({ type: 'danger', message: '服务器繁忙，请稍后再试' });
          console.log(err)
        })
    }).catch(() => {

    });
  },

  data: {
    uid: '',
    classItems: [],
    applyList: [],
    status: '',
    active: 0
  },

  onLoad: function(options) {
    
  },

  onShow: function() {
    this.setData({
      uid: wx.getStorageSync('uid'),
      status: wx.getStorageSync('status')
    })
    this._getClassInfo();
    this._getApplyList();
  },

  onHide: function() {

  },

  onUnload: function() {

  },

  onPullDownRefresh: function() {
    this._getClassInfo();
    this._getApplyList();
    wx.stopPullDownRefresh();
  },

  onReachBottom: function() {

  },
})